package com.arch.dev.client.view;

import java.io.File;

import com.arch.dev.client.controller.BudgetController;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FormHandler;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormSubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormSubmitEvent;
import com.google.gwt.user.client.ui.Widget;

public class UpLoadExcelDialogView extends DialogBox {

	private static UpLoadExcelDialogViewUiBinder uiBinder = GWT
			.create(UpLoadExcelDialogViewUiBinder.class);

	interface UpLoadExcelDialogViewUiBinder extends
			UiBinder<Widget, UpLoadExcelDialogView> {
	}

	private Widget widget;
	
	@SuppressWarnings("deprecation")
	public UpLoadExcelDialogView(Widget widget) {
		super(false, true);
		this.widget = widget;
		formPanel = new FormPanel();
		
		
		Widget w = uiBinder.createAndBindUi(this);
		uploadFormPanel = new UploadFormPanel();
		formPanel.add(uploadFormPanel);
		
		setText("Upload Excel File");
		setSize("100%", "100%");
		setGlassEnabled(true);
		
		formPanel.setAction("servlet/excel/upload");
		formPanel.setEncoding(FormPanel.ENCODING_MULTIPART);
		formPanel.setMethod(FormPanel.METHOD_POST);
		
		formPanel.addFormHandler(new FormHandler() {
			
	
			public void onSubmitComplete(FormSubmitCompleteEvent event) {
				
				
				UpLoadExcelDialogView.this.hide();
				String excelFileName = event.getResults();
				BudgetController.getInstance().processDataFromExcel(UpLoadExcelDialogView.this.widget, excelFileName);
				
			}
			
		
			public void onSubmit(FormSubmitEvent event) {
				
				
				
			}
		});
		
		uploadFormPanel.addUploadClickHandler(new ClickHandler() {
			
		
			public void onClick(ClickEvent event) {
				
				formPanel.submit();
			}
		});
		
		uploadFormPanel.addCancelClickHandler(new ClickHandler() {
			
	
			public void onClick(ClickEvent event) {
				
				UpLoadExcelDialogView.this.hide();
			}
		});
		// setAnimationEnabled(true);
		setWidget(w);
	}

	
	UploadFormPanel uploadFormPanel;
	
	@UiField(provided=true)
	FormPanel formPanel;
	
	public void show()

	{
		int ph = getOffsetHeight();

		int pw = getOffsetWidth();

		int wh = Window.getClientHeight();

		int ww = Window.getClientWidth();

		int locx = (ww - pw - getOffsetWidth()) / 3;

		int locy = (wh - ph - getOffsetHeight()) / 3;

		super.setPopupPosition(locx, locy);

		super.show();

	}

}
